<?php
class Tema extends AppModel {

	var $name = 'Tema';
	var $primaryKey = 'tema';
	var $validate = array(
		'nombre' => array(
			'isUnique' => array(
				'rule' => 'isUnique',
				'required' => true,
				'message' => 'El nombre del tema ya existe'),
			'notEmpty' => array(	
				'rule' => 'notEmpty',
				'required' => true,
				'message' => 'El nombre del tema no puede estar vacio')
			)
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'User' => array('className' => 'User',
								'foreignKey' => 'user_id',
								'conditions' => '',
								'fields' => '',
								'order' => ''
			)
	);

	var $hasMany = array(
			'Topico' => array('className' => 'Topico',
								'foreignKey' => 'tema_id',
								'dependent' => false,
								'conditions' => '',
								'fields' => '',
								'order' => '',
								'limit' => '',
								'offset' => '',
								'exclusive' => '',
								'finderQuery' => '',
								'counterQuery' => ''
			)
	);


	function listaTopicosdelTema($id = null){
		
		 //Le paso al modelo el id del Tema. Busca los topicos con dicho id.
		$topicos=$this->Topico->find('all',array('conditions' => array('Topico.tema_id' => $id)));
		//print_r($topicos);
		foreach ($topicos as $topico){
			$topic_info=$this->User->findByUser($topico['Topico']['user_id']);
			//print_r($topic_info);
			$topicos_username[$topico['Topico']['topico']]=$topic_info['User']['username'];
		}
		print_r($topicos_username);
		return $topicos_username;
	}
	
	function cantTopicosTodosTemas(){
		//me trae todos los temas en el foro, me trae su id y su nombre
		//$temas = $this->find('list',array('fields'=>array('Tema.tema','Tema.nombre')));
		$temas = $this->find('list'); 
		//print_r($temas);
		//return $temas;
		foreach ($temas as $temaid){
			$lista[$temaid]=$this->Topico->find('count',array('conditions' => array('Topico.tema_id =' => $temaid)));
		}
		print_r($lista);
		return $lista; //devuelve un array(index => valor)
			// donde "index" es el IdTema y "valor"	es la cantidad de topicos que posee dicho IdTema 
	}
	
}
?>